/*
 * @Copyright: 2016 www.yyfax.com Inc. All rights reserved.
 */
package com.yyfax.pay.order.data.dao.impl;

import com.yyfax.framework.dao.impl.BaseDaoImpl;
import com.yyfax.pay.order.data.dao.PayoutOrderDao;
import com.yyfax.pay.order.model.po.PayoutOrderPO;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 代付订单持久化实现
 *
 * @author buyi
 * @date 2017年04月12日 11:09:15
 * @since 1.0.0
 */
@Repository
public class PayoutOrderDaoImpl extends BaseDaoImpl<PayoutOrderPO, Integer> implements PayoutOrderDao {
	/** 表名，该值要和payout_order.xml的属性"tableName"保持一致 */
	private static final String TABLE_NAME = "payout_order";

	/** 方法名，该值要和payout_order.xml的属性"id"保持一致 */
	private final String SQL_INSERT = "insert";

	/** 方法名，该值该值要和payout_order.xml的属性"id"保持一致 */
	private final String SQL_SELECT_BY_ID = "selectById";

	private final String SQL_SELECT_BY_PAYOUT_ORDER_ID = "selectByPayoutOrderId";

	private final String SQL_SELECT_BY_ORDERID_AND_MERCHANTCODE = "selectByOrderIdAndMerchantCode";

	private final String SQL_UPDATE = "update";

	private final String SQL_UPDATE_STATE_BY_PAYOUT_ORDER_ID = "updateStateByPayoutOrderId";

	private final String SQL_UPDATE_STATE = "updateState";

	private final String SQL_SELECT_BY_MERCHANTCODE_AND_PAYOUTBATCHORDERID = "selectByMerchantCodeAndPayoutBatchOrderId";

	public PayoutOrderDaoImpl() {
		super(TABLE_NAME);
	}

	@Override
	public int insert(PayoutOrderPO po) {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_INSERT);
		BeanPropertySqlParameterSource sqlParameterSource = new BeanPropertySqlParameterSource(po);
		return namedParameterJdbcTemplate.insertForPK(sql, sqlParameterSource);
	}

	@Override
	public PayoutOrderPO selectByIdAndMerchantCode(int id, String merchantCode) {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_SELECT_BY_ID);
		MapSqlParameterSource params = new MapSqlParameterSource();
		params.addValue("id", id);
		return namedParameterJdbcTemplate.queryForObject(sql, params, new BeanPropertyRowMapper<PayoutOrderPO>(poClass));
	}

	@Override
	public PayoutOrderPO selectByOrderIdAndMerchantCode(String orderId, String merchantCode) {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_SELECT_BY_ORDERID_AND_MERCHANTCODE);
		MapSqlParameterSource params = new MapSqlParameterSource();
		params.addValue("orderId", orderId);
		params.addValue("merchantCode", merchantCode);
		return namedParameterJdbcTemplate.queryForObject(sql, params, new BeanPropertyRowMapper<PayoutOrderPO>(poClass));
	}

	@Override
	public int update(PayoutOrderPO po) {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_UPDATE);
		BeanPropertySqlParameterSource sqlParameterSource = new BeanPropertySqlParameterSource(po);
		return namedParameterJdbcTemplate.update(sql, sqlParameterSource);
	}

	@Override
	public List<PayoutOrderPO> selectByMerchantCodeAndPayoutBatchOrderId(String merchantCode, String payoutBatchOrderId) {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_SELECT_BY_MERCHANTCODE_AND_PAYOUTBATCHORDERID);
		MapSqlParameterSource params = new MapSqlParameterSource();
		params.addValue("merchantCode", merchantCode);
		params.addValue("payoutBatchOrderId", payoutBatchOrderId);
		return namedParameterJdbcTemplate.query(sql, params, new BeanPropertyRowMapper<PayoutOrderPO>(poClass));
	}

	@Override
	public int updateStateByPayoutOrderId(String payoutOrderId, String state, Integer rspCode, String rspMsg) {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_UPDATE_STATE_BY_PAYOUT_ORDER_ID);
		MapSqlParameterSource params = new MapSqlParameterSource();
		params.addValue("payoutOrderId", payoutOrderId);
		params.addValue("state", state);
		params.addValue("rspCode", rspCode);
		params.addValue("rspMsg", rspMsg);
		return namedParameterJdbcTemplate.update(sql, params);
	}

	@Override
	public int updateState(String payoutOrderId, String origState, String destState, int rspCode, String rspMsg) {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_UPDATE_STATE);
		MapSqlParameterSource params = new MapSqlParameterSource();
		params.addValue("payoutOrderId", payoutOrderId);
		params.addValue("origState", origState);
		params.addValue("destState", destState);
		params.addValue("rspCode", rspCode);
		params.addValue("rspMsg", rspMsg);
		return namedParameterJdbcTemplate.update(sql, params);
	}

	@Override
	public PayoutOrderPO selectByPayoutOrderId(String payoutOrderId) {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_SELECT_BY_PAYOUT_ORDER_ID);
		MapSqlParameterSource params = new MapSqlParameterSource();
		params.addValue("payoutOrderId", payoutOrderId);
		return namedParameterJdbcTemplate.queryForObject(sql, params, new BeanPropertyRowMapper<PayoutOrderPO>(poClass));
	}

	@Override
	public int updateRefundState(String payoutOrderId, String state, int rspCode, String rspMsg, String origIsRefund, String destIsRefund) {
		String sql = sqlFactory.getSQL(TABLE_NAME, "updateRefundState");
		MapSqlParameterSource params = new MapSqlParameterSource();
		params.addValue("payoutOrderId", payoutOrderId);
		params.addValue("state", state);
		params.addValue("rspCode", rspCode);
		params.addValue("rspMsg", rspMsg);
		params.addValue("origIsRefund", origIsRefund);
		params.addValue("destIsRefund", destIsRefund);
		return namedParameterJdbcTemplate.update(sql, params);
	}
}
